package setAPI.job;

import com.google.common.collect.Lists;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.operators.FlatMapOperator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * @author yue.cao
 * @since 10-13-2020
 */
public class BatchWordCount {

	public static void main(String[] args) throws Exception {
		ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
		DataSource<String> source = env.fromCollection(Lists.newArrayList("A", "B", "C", "D","A"));

		FlatMapOperator<String, Tuple2<String, Integer>> wordOne = source.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
			@Override
			public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
				out.collect(Tuple2.of(value, 1));
			}
		});
		//不同于stream是通过keyBy
		wordOne.groupBy(0).sum(1).print();
	}
}
